<template>
    <div>
        <rui-page :defines="defines">
            <rui-query :label-width="150"
                       ref="searchForm"
                       :query="queryData"
                       :query-rows="searchFormOptions"
            />
            <rui-table
                :defineId="'defineStaySettle'"
                :fixedLeft="['action']"
                :showIndex="false"
                :columns="staySettleColumns"
                :slots="[{key:'action',slot:'action'},{key:'applyNo',slot:'applyNo'}]"
                :select="false"
                @loadDatas="queryData"
                ref-query="searchForm"
                ref="planTable"
            >
                <template slot="toolBar">
                    <Button @click="add" icon="md-add" type="primary">新增</Button>
                </template>
                <template slot-scope="{ row }" slot="action">
                    <div>
                        <Button :disabled="!row.isViewBtn" type="primary" size="small"
                                @click="handle(row,'view','edit')">处理
                        </Button>
                        <Button :disabled="row.processStatus !== 'draft'" type="primary" size="small"
                                @click="handle(row,'edit','view')">编辑
                        </Button>
                        <Button :disabled="row.processStatus !== 'draft'" type="error" size="small"
                                @click="deleate(row)">删除
                        </Button>
                    </div>
                </template>
                <template slot-scope="{ row, index }" slot="applyNo">
                    <a @click.prevent="handle(row,'view','view')">
                        {{row.applyNo}}
                    </a>
                </template>
            </rui-table>
        </rui-page>

        <ActivatedCase
            :getDataFn="getDataFn"
            :isSP="isSP"
            :modal="modal"
            :handleBusiness="''"
            modalTitle="已激活案件列表"
            :dataDict="dataDict"
            @showContractDetial="showContractDetial"
            @closeModal="closeModal"
            :contractBusiness="contractBusiness"
        />

    </div>
</template>

<script>

    import ActivatedCase from "_p/basic/pages/contract/common/activated-case/index";
    import {findActivatedContractList} from "_p/basic/api/contract/post-loan-info-change";
    import {channelType, loanCommissioner} from "_p/basic/api/contract/channel-type/channel-type";
    import {mapGetters} from "vuex";

    import staySettleDefine from "./define/stay-settle-define";
    import staySettleQueryDefine from "./define/stay-settle-query-define";

    import {
        getManagePrepaymentApply,
        delManagePrepaymentApplyById
    } from "@/projects/basic/api/contract/clearance/stay-settle";

    export default {
        components: {
            ActivatedCase
        },
        name: "staySettle",
        data() {
            return {
                searchFormOptions: [
                    {
                        defineId: "defineQueryStaySettle",
                        span: 5,
                        fields: ["applyNo", 'contractNo', 'custName', 'processStatus', 'businessSource', 'repayMode', 'prepaymentRequestTimeQuery', 'prepaymentApprovedTimeQuery', 'collectionStatus']
                    },
                ],
                staySettleColumns: [
                    'action', 'applyNo', 'contractNo', 'custName', 'processStatus', 'businessSource', 'alreadyPeriods', 'repayMode', 'currentContractStatus', 'prepaymentRequestTime', 'prepaymentApprovedTime', 'collectionStatus'
                ],
                //试算页面
                businessSource: 'BUSINESS_PEOPLE',
                //弹窗页面需要数据
                isSP: false,
                modal: false,
                dataDict: {}, // 数据字典
                contractBusiness: [""],
            }
        },
        methods: {
            //刷新表格
            refresh() {
                this.$refs['planTable'].reloadData();
            },
            //初始化查询
            queryData(queryData) {
                getManagePrepaymentApply(queryData).then(res => {
                    if (res.code === "0000") {
                        let {records, total} = res.data;
                        this.$refs.planTable.updateTableData(records, total);
                    }
                });
            },
            //删除
            deleate(row) {
                delManagePrepaymentApplyById(row.id).then(res => {
                    if (res.code === "0000") {
                        this.refresh();
                    }
                });
            },
            //处理
            handle(row, op, auditOp) {
                row.op = op;
                row.auditOp = auditOp;
                row.applyId = row.id;
                row.isApprove = row.caseNo != null ? true : false;//根据案件编号判断是否在审核中
                row.isSP = this.isSP;
                row.isDh = this.isDh;
                row.businessSource = this.businessSource;
                row.applyDate = row.prepaymentRequestTime;
                row.endDate = row.prepaymentApprovedTime;
                row.applyBy = row.createBy;
                row.type = "advanceSettle";
                // console.info("处理信息："+JSON.stringify(row));
                this.afs.newTab(this, 'projects/basic/pages/contract/clearance/apply-settle', '提前还款试算处理', 'ios-add', row, row.contractNo, [], true);
            },
            //新增打开弹窗
            add() {
                this.modal = true;
            },
            //关闭弹窗
            closeModal() {
                this.modal = false;
            },
            //弹窗回调方法
            showContractDetial(row) {
                if (!row && !row.contractNo) return;
                this.modal = false;
                // console.info("弹窗给出的值："+JSON.stringify(row))
                row.op = "add";
                row.isApprove = false;
                row.isSP = this.isSP;
                row.isDh = this.isDh;
                row.businessSource = this.businessSource;
                row.type = "advanceSettle";
                // console.info("弹窗组装后给出的值："+JSON.stringify(row))
                //打开试算页面
                this.afs.newTab(this, 'projects/basic/pages/contract/clearance/apply-settle', '提前还款试算新增', 'ios-add', row, row.contractNo, [], true);
            },
        },
        mounted() {
        },
        activated() {
            this.refresh();
        },
        created() {
            this.isSP = channelType(this.$route.meta.params);
            if (this.isSP) {
                this.searchFormOptions = [
                    {
                        defineId: "defineQueryStaySettle",
                        span: 5,
                        fields: ["applyNo", 'custName', 'processStatus', 'repayMode', 'prepaymentRequestTimeQuery', 'collectionStatus']
                    },
                ];
                this.staySettleColumns = [
                    'action', 'applyNo', 'custName', 'processStatus', 'alreadyPeriods', 'repayMode', 'currentContractStatus', 'prepaymentRequestTime', 'prepaymentApprovedTime', 'collectionStatus'
                ];
                this.businessSource = 'SP';
            }
        },
        computed: {
            defines() {
                return [
                    {
                        id: "defineStaySettle",
                        fields: staySettleDefine
                    },
                    {
                        id: "defineQueryStaySettle",
                        fields: staySettleQueryDefine
                    }
                ];
            },
            ...mapGetters({
                userInfo: "userInfo",
                roleType: "roles"
            }),
            isDh: function () {
                return loanCommissioner(this.roleType);
            },
            getDataFn: () => findActivatedContractList
        },
    }
</script>

<style scoped>

</style>
